//
//  ViewExtention.swift
//  WFCommunity
//
//  Created by 白翊 on 2020/10/20.
//  Copyright © 2020 白翊. All rights reserved.
//

import UIKit

extension UIImageView {
    
    public convenience init(image: String? = nil, contentMode: UIView.ContentMode, frame: CGRect? = nil) {
        self.init()
        if let frame = frame {
            self.frame = frame
        }
        self.contentMode = contentMode
        if let image = image, image.count > 0 {
            self.image = UIImage(named: image)
        }
    }
}

extension UIView {
    func setBGGradient() {
        self.backgroundColor = UIColor.clear
        
        if self.layer.sublayers?.last?.isKind(of: CAGradientLayer.self) ?? false {
            self.layer.sublayers?.last?.removeFromSuperlayer();
        }
        
        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = [UIColor.themeLight.cgColor, UIColor.theme.cgColor, UIColor(hexString: "fc6060")!.cgColor, UIColor(hexString: "fb4e4e")!.cgColor]
        gradientLayer.startPoint = CGPoint.zero
        gradientLayer.endPoint = CGPoint.init(x: 1, y: 0)
        gradientLayer.frame = self.bounds
        gradientLayer.cornerRadius = self.layer.cornerRadius
        gradientLayer.masksToBounds = true
        gradientLayer.zPosition = -1
        self.layer.addSublayer(gradientLayer)
    }
    
    func setVerticalGradient(topColor: UIColor, btmColor: UIColor) {
        self.backgroundColor = UIColor.clear
        
        if self.layer.sublayers?.last?.isKind(of: CAGradientLayer.self) ?? false {
            self.layer.sublayers?.last?.removeFromSuperlayer();
        }
        
        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = [topColor.cgColor,btmColor.cgColor]
        gradientLayer.startPoint = CGPoint.zero
        gradientLayer.endPoint = CGPoint.init(x: 0, y: 1)
        gradientLayer.frame = self.bounds
        gradientLayer.cornerRadius = self.layer.cornerRadius
        gradientLayer.masksToBounds = true
        gradientLayer.zPosition = -1
        self.layer.addSublayer(gradientLayer)
    }
    
    func setHorizontalGradient(leftColor: UIColor, rightColor: UIColor) {
        self.backgroundColor = UIColor.clear
        
        if self.layer.sublayers?.last?.isKind(of: CAGradientLayer.self) ?? false {
            self.layer.sublayers?.last?.removeFromSuperlayer();
        }
        
        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = [leftColor.cgColor,rightColor.cgColor]
        gradientLayer.startPoint = CGPoint.zero
        gradientLayer.endPoint = CGPoint.init(x: 1, y: 0)
        gradientLayer.frame = self.bounds
        gradientLayer.cornerRadius = self.layer.cornerRadius
        gradientLayer.masksToBounds = true
        gradientLayer.zPosition = -1
        self.layer.addSublayer(gradientLayer)
        
        self.layer.masksToBounds = false
        self.clipsToBounds = false
        self.layer.shadowColor = leftColor.cgColor
        self.layer.shadowRadius = 6
        self.layer.shadowOpacity = 0.5
        self.layer.shadowOffset = CGSize.zero
        self.layer.shadowPath = UIBezierPath.init(roundedRect: self.bounds, cornerRadius: self.layer.cornerRadius).cgPath
    }
    
    func setThemeGradient() {
        self.setHorizontalGradient(leftColor: .themeLight, rightColor: .theme);
    }
    
    func setBorder(width: CGFloat, color: UIColor) {
        layer.borderColor = color.cgColor
        layer.borderWidth = width
    }
    
    func setCornerRadius(radius: CGFloat) {
        layer.cornerRadius = radius
        clipsToBounds = true
    }
}

extension UILabel {
    public convenience init(string: String? = nil, fontSize: CGFloat? = nil, frame: CGRect? = nil, color: UIColor? = nil, textAlignment: NSTextAlignment = .left){
        self.init()
        if let frame = frame {
            self.frame = frame
        }
        self.text = string
        self.numberOfLines = 0;
        self.textColor = color
        self.textAlignment = textAlignment
        
        if let fontSize = fontSize {
            self.font = UIFont.systemFont(ofSize: fontSize)
        }
    }
}

extension UIButton {
    public convenience init(title: String? = nil, titleColor: UIColor? = nil, fontSize: CGFloat? = nil, frame: CGRect? = nil, handle: ((UIButton) -> Void)? = nil) {
        self.init()
        if let frame = frame {
            self.frame = frame
        }
        
        if let title = title {
            self.setTitle(title, for: UIControl.State.normal)
        }
        
        if let titleColor = titleColor {
            self.setTitleColor(titleColor, for: UIControl.State.normal)
        }
        
        if let fontSize = fontSize {
            self.titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        }
        
        self.addBlock(for: .touchUpInside) { (sender) in
            if let sender = sender as? UIButton {
                handle?(sender)
            }
        }
    }
}

extension UIBarButtonItem {
    class func createItem(img: String, target: Any, selector: Selector) -> UIBarButtonItem {
        let btn_size: CGFloat = 32
        let item = UIButton(type: .custom)
        item.frame = CGRect(x: 0, y: 0, width: btn_size, height: btn_size)
        item.setImage(UIImage(named: img)?.byTintColor(.title), for: .normal)
        item.setCornerRadius(radius: btn_size * 0.5)
        item.backgroundColor = .light
        item.addTarget(target, action: selector, for: .touchUpInside)
        return UIBarButtonItem(customView: item)
    }
}
